#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2022/10/23 23:37
# @Author  : when
# @File    : boss_utils.py
# @Description : 工具类

import hashlib

from crawler.orm_model.boss_job import BossJob


def gen_job_flag(boss_job: BossJob):
    """
    生成boss_job表唯一标识
    :param boss_job:
    :return:
    """
    # 数据唯一标识，md5(name + company_name + salary + experience + degree + skill_tag + 招聘人)
    flag_str = f"{boss_job.name}|{boss_job.company_name}|{boss_job.company_area}|{boss_job.salary}|" \
               f"{boss_job.experience}|{boss_job.degree}|{boss_job.skill_tag}"
    return hashlib.md5(flag_str.encode("utf8")).hexdigest()


def card_to_boss(boss_job, job_card):
    """
    从html解析的对象中获取，数据入库
    :param boss_job:
    :param job_card:
    :return:
    """
    boss_job.name = job_card.select_one(".job-name").get_text()
    boss_job.company_name = job_card.select_one(".company-name").get_text()
    boss_job.company_area = job_card.select_one(".job-area").get_text()
    boss_job.company_tag = job_card.select_one(".company-tag-list").get_text(separator="|", strip=True)
    boss_job.salary = job_card.select_one(".salary").get_text()
    edu_soup = job_card.select_one(".job-info").select_one(".tag-list")
    edu_soup_list = [i for i in edu_soup.stripped_strings]
    boss_job.experience = edu_soup_list[0]
    boss_job.degree = edu_soup_list[-1]
    skill_tag_soup = job_card.select_one(".job-card-footer").select_one(".tag-list")
    boss_job.skill_tag = "|".join(skill_tag_soup.stripped_strings)
    return boss_job, job_card
